package com.jt.tenant.activity;


import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
//import com.jt.tenant.conn.GetInsPayCheck;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetNewInsSUP;
import com.jt.tenant.widget.MyListView;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.io.Serializable;


public class InsConfirmOrderActivity extends JTActivity implements View.OnClickListener, Serializable {

    private TextView title_name, commit, next;

    private LinearLayout call;

    private TextView plate, name, engine, vin, model, price, check;//车牌号，车主姓名，发动机号，车架号，品牌型号，保费总价,查看保单详情

    private TextView insurant_name, insurant_idcard, insurant_phone, insurant_email;//被保险人姓名、身份证号、电话、电子邮箱

    private TextView insure_name, insure_idcard, insure_phone, insure_email;//投保人姓名、身份证号、电话、电子邮箱

    private TextView consignee_way, consignee_name, consignee_phone, consignee_address;//收件人 方式、姓名、电话、地址

//    积分抵扣相关

    private ImageView use_integral_ty;

    private TextView tv_use_integral_ty;

    private EditText et_inspay_ty;

    private TextView tv_inspay_tydk;

    private ImageView use_integral_dx;

    private TextView tv_use_integral_dx;

    private EditText et_inspay_dx;

    private TextView tv_inspay_dxdk;

    private TextView pay;//实际支付金额tv_ins_confirm_pay

    private double allMoney = 0;//总合计

//    private List<SetNewInsSUP.VCIList> mLists = new ArrayList<>();

    private int flag = 0;//根据标识判断状态

    private int use_inte_ty = 0, use_inte_dx = 0;

    private String order_id, money;

    private int jf1, jf2, total_jf;

    private int use_jf1, use_jf2;

    private Double dx, dx1, dx2;

    private int jf_flag = 0;

    private boolean first_resume = false;

//    private int fail_reason = 0;

    private SetNewInsSUP setNewInsSUP = new SetNewInsSUP(new AsyCallBack<SetNewInsSUP.Info>() {

        @Override
        public void onSuccess(String toast, int type, SetNewInsSUP.Info info) throws Exception {
            super.onSuccess(toast, type, info);

            if (info.msg.equals("000")) {

                if(info.mInsItemList != null) {

                    for (int i = 0; i < info.mInsItemList.size(); i++){

                        allMoney += Double.parseDouble(info.mInsItemList.get(i).price);

                        allMoney += Double.parseDouble(info.mInsItemList.get(i).flag_price);

                    }

                }

                initData(info);

                order_id = info.ins_id;

            } else {

                UtilToast.show(context, info.msg);

            }


        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);
        }

    });
//
//    private GetInsPayCheck getInsPayCheck = new GetInsPayCheck(new AsyCallBack<GetInsPayCheck.Info>() {
//
//        @Override
//        public void onSuccess(String toast, int type, GetInsPayCheck.Info info) throws Exception {
//
//            super.onSuccess(toast, type, info);
//
//            if (info.msg.equals("000")) {
//
//                Intent intent = new Intent(context, GSPaymentActivity.class);
//
//                intent.putExtra("order_id", order_id);
//
//                if((allMoney-dx1-dx2)>0) {
//
//                    intent.putExtra("total_price", Tools.F_num(String.valueOf(allMoney - dx1 - dx2)));
//
//                }else {
//
//                    intent.putExtra("total_price", "0,01");
//
//                }
//
//                intent.putExtra("flag", "4");
//
//                startActivity(intent);
//
//            } else {
//
//                UtilToast.show(context, info.msg);
//
//            }
//        }
//    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ins_confirm_order);

        initview();

    }

    @Override
    protected void onResume() {

//        mLists.clear();

        super.onResume();


    }

    private void initview() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("保单确认");

        (commit = (TextView) findViewById(R.id.commit)).setText("取消查询");

        (call = (LinearLayout) findViewById(R.id.lin_ins_call)).setOnClickListener(this);

        (next = (TextView) findViewById(R.id.tv_ins_confirm_next)).setOnClickListener(this);

        plate = (TextView) findViewById(R.id.tv_ins_confirm_plate);

        name = (TextView) findViewById(R.id.tv_ins_confirm_name);

        engine = (TextView) findViewById(R.id.tv_ins_confirm_engine);

        vin = (TextView) findViewById(R.id.tv_ins_confirm_vin);

        model = (TextView) findViewById(R.id.tv_ins_confirm_model);

        price = (TextView) findViewById(R.id.tv_ins_confirm_price);

        (check = (TextView) findViewById(R.id.tv_ins_confirm_check)).setOnClickListener(this);

        insurant_name = (TextView) findViewById(R.id.tv_ins_confirm_insurant_name);

        insurant_idcard = (TextView) findViewById(R.id.tv_ins_confirm_insurant_idcard);

        insurant_phone = (TextView) findViewById(R.id.tv_ins_confirm_insurant_phone);

        insurant_email = (TextView) findViewById(R.id.tv_ins_confirm_insurant_email);

        insure_name = (TextView) findViewById(R.id.tv_ins_confirm_insure_name);

        insure_idcard = (TextView) findViewById(R.id.tv_ins_confirm_insure_idcard);

        insure_phone = (TextView) findViewById(R.id.tv_ins_confirm_insure_phone);

        insure_email = (TextView) findViewById(R.id.tv_ins_confirm_insure_email);

        consignee_way = (TextView) findViewById(R.id.tv_ins_confirm_consignee_way);

        consignee_name = (TextView) findViewById(R.id.tv_ins_confirm_consignee_name);

        consignee_phone = (TextView) findViewById(R.id.tv_ins_confirm_consignee_phone);

        consignee_address = (TextView) findViewById(R.id.tv_ins_confirm_consignee_address);

        use_integral_ty = (ImageView) findViewById(R.id.use_integral_ty);

        use_integral_ty.setOnClickListener(this);

        tv_use_integral_ty = (TextView) findViewById(R.id.tv_use_integral_ty);

        et_inspay_ty = (EditText) findViewById(R.id.et_inspay_ty);

        tv_inspay_tydk = (TextView) findViewById(R.id.tv_inspay_tydk);

        use_integral_dx = (ImageView) findViewById(R.id.use_integral_dx);

        use_integral_dx.setOnClickListener(this);

        tv_use_integral_dx = (TextView) findViewById(R.id.tv_use_integral_dx);

        et_inspay_dx = (EditText) findViewById(R.id.et_inspay_dx);

        tv_inspay_dxdk = (TextView) findViewById(R.id.tv_inspay_dxdk);

        pay = (TextView) findViewById(R.id.tv_ins_confirm_pay);

        setNewInsSUP.flag = "3";

        setNewInsSUP.execute(this);

    }

    private void initData(SetNewInsSUP.Info info) {

        plate.setText(info.baoxianOrder.plate_num);

        name.setText(info.baoxianOrder.owner_name);

        engine.setText(info.baoxianOrder.engine);

        vin.setText(info.baoxianOrder.vin);

        model.setText(info.baoxianOrder.car_xh);

        price.setText(Tools.F_num(String.valueOf(allMoney))+"元");

        insurant_name.setText(info.baoxianOrder.owner_name);

        insurant_idcard.setText(info.baoxianOrder.idcard);

        insurant_phone.setText(info.baoxianOrder.phone);

        insurant_email.setText(info.baoxianOrder.email);

        insure_name.setText(info.baoxianOrder.phname);

        insure_idcard.setText(info.baoxianOrder.phidcard);

        insure_phone.setText(info.baoxianOrder.phphone);

        insure_email.setText(info.baoxianOrder.phemail);

        if (info.baoxianOrder.out_type.equals("0")) {

            consignee_way.setText("电子保单");

        } else {

            consignee_way.setText("纸质保单");

        }

        consignee_name.setText(info.baoxianOrder.recipients);

        consignee_phone.setText(info.baoxianOrder.recipients_phone);

        consignee_address.setText(info.baoxianOrder.accept_address);

        jf1 = (int) (Double.parseDouble(JTApplication.jtPreferences.readUserIntegral("1")));

        jf2 = (int) (Double.parseDouble(JTApplication.jtPreferences.readUserIntegral("2")));

        //如果走完接口，通用积分和定向积分使用为0，则初始化，处理抵扣；不为0，代表已经完成抵扣，不可修改
        if (info.delIntegralCount.equals("0") && info.delCommonCount.equals("0")) {

            //积分抵扣初始化
            use_jf1 = jf1;

            use_jf2 = jf2;

            dx = Double.valueOf(jf1 + jf2) / 10;

            dx1 = Double.valueOf(jf1) / 10;

            dx2 = Double.valueOf(jf2) / 10;

            if (dx > allMoney) {

                if (dx1 >= allMoney) {

                    if (allMoney < 0.1) {

                        use_jf1 = 1;

                        dx1 = 0.1;

                    } else {

                        use_jf1 = (int) (allMoney * 10);

                        dx1 = Double.valueOf(use_jf1) / 10;

                    }

                    use_jf2 = 0;

                    dx2 = 0.0;

                } else {

                    if (allMoney - dx1 < 0.1) {

                        use_jf2 = 1;

                    } else {

                        use_jf2 = (int) ((allMoney - dx1) * 10);

                    }

                    dx1 = Double.valueOf(use_jf1) / 10;

                    dx2 = allMoney - dx1;

                }

            }

            tv_use_integral_dx.setText("当前可用定向积分：" + String.valueOf(jf1) + "，使用");

            et_inspay_dx.setText(String.valueOf(use_jf1));

            tv_inspay_dxdk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx1)) + "元");

            tv_use_integral_ty.setText("当前可用通用积分：" + String.valueOf(jf2) + "，使用");

            et_inspay_ty.setText(String.valueOf(use_jf2));

            tv_inspay_tydk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx2)) + "元");

            use_integral_dx.setImageResource(R.mipmap.jian);

            use_inte_dx = 1;

            use_integral_ty.setImageResource(R.mipmap.jian);

            use_inte_ty = 1;

//                定向抵扣关联逻辑
            et_inspay_dx.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {

                    if (hasFocus) {

                        jf_flag = 1;

                    } else {

                        jf_flag = 0;

                    }

                }
            });

            et_inspay_dx.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    if (jf_flag == 1) {

                        if (!String.valueOf(s).equals("") && !String.valueOf(s).equals("0")) {

                            Deduction(s.toString().trim(), 1);

                        } else {

                            Deduction("0", 1);

                        }

                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }

            });

//                通用抵扣关联逻辑
            et_inspay_ty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {

                    if (hasFocus) {

                        jf_flag = 2;

                    } else {

                        jf_flag = 0;

                    }

                }
            });

            et_inspay_ty.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    if (jf_flag == 2) {

                        if (!String.valueOf(s).equals("") && !String.valueOf(s).equals("0")) {

                            Deduction(s.toString().trim(), 2);

                        } else {

                            Deduction("0", 2);

                        }

                    }

                }


                @Override
                public void afterTextChanged(Editable s) {

                }
            });

        } else {

            use_jf1 = (int) (Double.parseDouble(info.delIntegralCount));

            use_jf2 = (int) (Double.parseDouble(info.delCommonCount));

            dx1 = Double.valueOf(use_jf1) / 10;

            dx2 = Double.valueOf(use_jf2) / 10;

            tv_use_integral_dx.setText("已使用定向积分：" + String.valueOf(use_jf1));

            et_inspay_dx.setVisibility(View.GONE);

            tv_inspay_dxdk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx1)) + "元");

            use_integral_dx.setEnabled(false);

            use_integral_dx.setImageResource(R.mipmap.jian);

            use_inte_dx = 0;

            tv_use_integral_ty.setText("已使用通用积分：" + String.valueOf(use_jf2));

            et_inspay_ty.setVisibility(View.GONE);

            tv_inspay_tydk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx2)) + "元");

            use_integral_ty.setEnabled(false);

            use_integral_ty.setImageResource(R.mipmap.jian);

            use_inte_ty = 0;

        }


    }

    @Override
    public void onClick(View v) {

        super.onClick(v);

        switch (v.getId()) {


//            case R.id.use_integral_ty:
//
//                if (use_inte_ty == 0) {
//
//                    use_integral_ty.setImageResource(R.mipmap.jian);
//
//                    use_inte_ty = 1;
//
//                    Deduction(et_inspay_ty.getText().toString().trim(),2);
//
//                } else {
//
//                    use_integral_ty.setImageResource(R.mipmap.jian1);
//
//                    use_inte_ty = 0;
//
//                    dx2 = 0.0;
//
//                    et_inspay_ty.setText("0");
//
//                    tv_inspay_tydk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx2)) + "元");
//
//
//                }
//
//                break;
//
//            case R.id.use_integral_dx:
//
//                if (use_inte_dx == 0) {
//
//                    use_integral_dx.setImageResource(R.mipmap.jian);
//
//                    use_inte_dx = 1;
//
//                    Deduction(et_inspay_dx.getText().toString().trim(),1);
//
//
//
//                } else {
//
//                    use_integral_dx.setImageResource(R.mipmap.jian1);
//
//                    use_inte_dx = 0;
//
//                    dx1 = 0.0;
//
//                    et_inspay_dx.setText("0");
//
//                    tv_inspay_dxdk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx1)) + "元");
//
//                }
//
//                break;
//
//            case R.id.pricing_pay:
//
//                if ((use_inte_ty == 0||et_inspay_ty.getText().toString().trim().equals("")||et_inspay_ty.getText().toString().trim().equals("0"))
//
//                        && (use_inte_dx == 0||et_inspay_dx.getText().toString().trim().equals("")||et_inspay_dx.getText().toString().trim().equals("0"))) {
//
//                    Intent intent = new Intent(this, GSPaymentActivity.class);
//
//                    intent.putExtra("order_id", order_id);
//
//                    intent.putExtra("total_price",Tools.F_num(String.valueOf(allMoney-dx1-dx2)));
//
//                    intent.putExtra("flag", "4");
//
//                    startActivity(intent);
//
//                } else {
//
//                    if(use_inte_dx == 1) {
//
//                        getInsPayCheck.integralCount = et_inspay_dx.getText().toString().trim().equals("")?"0":String.valueOf(et_inspay_dx.getText().toString().trim());
//
//                    }else {
//
//                        getInsPayCheck.integralCount = "0";
//
//                    }
//
//                    if(use_inte_ty == 1) {
//
//                        getInsPayCheck.commonCount = et_inspay_ty.getText().toString().trim().equals("")?"0":String.valueOf(et_inspay_ty.getText().toString().trim());
//
//                    }else {
//
//                        getInsPayCheck.commonCount = "0";
//
//                    }
//
//                    getInsPayCheck.execute(this);
//
//                }
//
//                break;
//
            case R.id.pricing_phone_text:

                Tools.Call(this, "4006115616");

                break;

            case R.id.tv_ins_confirm_check:

                Intent intent = new Intent(this,InsCheckSelectActivity.class);

                startActivity(intent);

                break;

//            case R.id.pricing_cancel:
//
//                InsCancelDialog dialog = new InsCancelDialog(context);
//
//                dialog.setTip("确定取消报价查询？如果取消将丢失已填写数据");
//
//                dialog.show();
//
//                break;
//
//            case R.id.pricing_wait_close:
//
//                onBack(v);
//
//                break;
//
//            case R.id.pricing_fail_close:
//
//                onBack(v);
//
//                break;
//
//            case R.id.pricing_again:
//
//                startActivity(new Intent(this, InsQuoteActivity.class));
//
//                break;

        }

    }

//    @Override
//    public void onRightClick(View v) {
//
//        super.onRightClick(v);
//
//        InsCancelDialog dialog = new InsCancelDialog(context);
//
//        dialog.setTip("确定取消报价查询？如果取消将丢失已填写数据");
//
//        dialog.show();
//    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            startActivity(new Intent(InsConfirmOrderActivity.this, MainActivity.class).putExtra("id", 1));

        }

        return true;
    }

    private void Deduction(String num, int location) {

        int input_jf;

        if (num.equals("") || num.equals(null)) {

            input_jf = 0;

        } else {

            input_jf = Integer.parseInt(num);

        }

        int used_jf = 0, new_jf = 0;

        int fact_jf = 0;

        if (location == 1) {

            used_jf = et_inspay_ty.getText().toString().trim().equals("") ? 0 : Integer.parseInt(et_inspay_ty.getText().toString().trim());

            dx2 = Double.parseDouble(String.valueOf(used_jf)) / 10;

            fact_jf = input_jf > jf1 ? jf1 : input_jf;

        } else {

            used_jf = et_inspay_dx.getText().toString().trim().equals("") ? 0 : Integer.parseInt(et_inspay_dx.getText().toString().trim());

            dx1 = Double.parseDouble(String.valueOf(used_jf)) / 10;

            fact_jf = input_jf > jf2 ? jf2 : input_jf;

        }

        if (used_jf >= allMoney * 10) {

            new_jf = 0;

        } else {

            if ((allMoney * 10 - used_jf) > fact_jf) {

                new_jf = fact_jf;

            } else {

                new_jf = (int) (allMoney * 10 - used_jf);

            }

        }


        if (location == 1) {

            if (input_jf != new_jf) {

                et_inspay_dx.setText(String.valueOf(new_jf));

            }

            dx1 = Double.valueOf(new_jf) / 10;

            tv_inspay_dxdk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx1)) + "元");

        } else {

            if (input_jf != new_jf) {

                et_inspay_ty.setText(String.valueOf(new_jf));

            }

            dx2 = Double.valueOf(new_jf) / 10;

            tv_inspay_tydk.setText("抵扣¥" + Tools.F_num(String.valueOf(dx2)) + "元");

        }

//        Log.e("INPUT_JF=========",String.valueOf(input_jf));
//
//        Log.e("NEW_JF=========",String.valueOf(new_jf));
//
//        Log.e("DX1=========",String.valueOf(dx1));
//
//        Log.e("ET1=========",et_inspay_dx.getText().toString().trim());
//
//        Log.e("DX2=========",String.valueOf(dx2));
//
//        Log.e("ET2=========",et_inspay_ty.getText().toString().trim());
//
//        Log.e("---------","-----------------------------------------------------------------------------------------");

    }

}
